package com.kj.tms.system.service.menu.serviceImpl;


import com.kj.pagehelper.Page;
import com.kj.tms.common.bean.dto.ReturnData;
import com.kj.tms.common.bean.exception.BusinessException;
import com.kj.tms.common.utils.constant.Consts;
import com.kj.tms.common.utils.uuid.IdGen;
import com.kj.tms.pri.system.menu.dto.DataSourceDTO;
import com.kj.tms.pri.system.menu.service.DataSourceService;
import com.kj.tms.system.service.menu.dao.DataSourceMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author zhaolj
 *
 */
@RestController
public class DataSourceServiceImpl implements DataSourceService {
    private static final Logger logger = LoggerFactory.getLogger(DataSourceServiceImpl.class);
    @Autowired
    private DataSourceMapper dataSourceMapper;

    @Override
    public ReturnData queryByPageInfo(@RequestBody DataSourceDTO model) {
        //设置页面数据
        Map<String, Object> returnObject = new HashMap<String, Object>();
        Page page = new Page(model.getPage(), model.getRows());
        model.setBeginIndex(page.getStartRow());
        model.setEndIndex(page.getEndRow());
        List<DataSourceDTO> dsList;
        int total;
        try {
            dsList = dataSourceMapper.listByPage(model);
            total = this.getTotalCounts(model);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnData(Consts.ERROR, "查询失败！", returnObject);
        }
        returnObject.put("total", total);
        returnObject.put("rows", dsList);
        return new ReturnData(Consts.SUCCESS, "", returnObject);
    }

    @Override
    public List<DataSourceDTO> queryByPage(@RequestBody DataSourceDTO model) {
        return dataSourceMapper.listByPage(model);
    }

    @Override
    public ReturnData getByCondition(@RequestBody DataSourceDTO model) {
        DataSourceDTO dto = dataSourceMapper.getByCondition(model);
        if (null == dto) {
            return new ReturnData(Consts.ERROR, "", dto);
        }
        return new ReturnData(Consts.SUCCESS, "", dto);
    }

    @Override
    public ReturnData save(@RequestBody DataSourceDTO dto) {
        try {
            dto.setId(IdGen.getId());
            dataSourceMapper.save(dto);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BusinessException(Consts.ERROR, "保存失败！", null);
        }
        return new ReturnData(Consts.SUCCESS, "保存成功！", null);
    }

    @Override
    public ReturnData delete(@RequestBody DataSourceDTO dto) {
        try {
            dataSourceMapper.delete(dto);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BusinessException(Consts.ERROR, "删除失败！", null);
        }
        return new ReturnData(Consts.SUCCESS, "删除成功！", null);
    }

    @Override
    public ReturnData update(@RequestBody DataSourceDTO dto) {
        try {
            dataSourceMapper.update(dto);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BusinessException(Consts.ERROR, "修改失败！", null);
        }
        return new ReturnData(Consts.SUCCESS, "修改成功！", null);
    }

    @Override
    public int getTotalCounts(@RequestBody DataSourceDTO model) {
        return dataSourceMapper.getTotalCounts(model);
    }

}
